package de.se2.sharebox.frames;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;

import de.se2.sharebox.classes.*;

/**
 *
 * @author Gruppe 10
 *
 */
public class MainFrame extends JFrame
{

	DefaultListModel dlm = new DefaultListModel();
	JLabel title;
	JList explorer;
	JButton createFolder;
	JButton uploadFile;
	JButton download;
	JButton rename;
	JButton cut;
	JButton copy;
	JButton paste;
	JButton delete;
	JButton sendInvitation;
	JButton showLog;
	JButton logout;
	JButton showProperties;

	/**
	 * Konstruktor
	 */
	public MainFrame()
	{
		InitForm();
	}

	public JButton getDownload()
	{
		return download;
	}

	public JButton getCreateFolder()
	{
		return createFolder;
	}

	public JButton getUploadFile()
	{
		return uploadFile;
	}

	public JButton getShowProperties()
	{
		return showProperties;
	}

	public JButton getRename()
	{
		return rename;
	}

	public JButton getCut()
	{
		return cut;
	}

	public JButton getCopy()
	{
		return copy;
	}

	public JButton getPaste()
	{
		return paste;
	}

	public JButton getDelete()
	{
		return delete;
	}

	public JButton getSendInvitation()
	{
		return sendInvitation;
	}

	public JButton getShowLog()
	{
		return showLog;
	}

	public JButton getLogout()
	{
		return logout;
	}

	public void setExplorer(Folder userFolder)
	{
		dlm.removeAllElements();
		Vector<Folder> folders = new Vector<Folder>();
		Vector<File> files = new Vector<File>();
		int index = 0;

		if (userFolder != null)
		{
			folders = userFolder.getFolderCollection();
			files = userFolder.getFileCollection();
		}
		if (folders != null)
		{
			for (Folder folder : folders)
			{
				dlm.add(index, folder.getName() + " - " + folder.getOwner().getEmailAdresse() + "(Besitzer)");
				index++;
			}
		}
		if (files != null)
		{
			for (File file : files)
			{
				dlm.add(index, file.getName());
				index++;
			}
		}

	}

	public JList getExplorer()
	{
		return explorer;
	}

	public void setExplorerSelectionListener(ListSelectionListener explorerSelectionListener)
	{
		this.explorer.addListSelectionListener(explorerSelectionListener);
	}

	public void setLogoutListener(ActionListener al)
	{
		this.logout.addActionListener(al);
	}

	public void setCreateFolderListener(ActionListener al)
	{
		this.createFolder.addActionListener(al);
	}

	public void setUploadFileListener(ActionListener al)
	{
		this.uploadFile.addActionListener(al);
	}

	public void setDownloadListener(ActionListener al)
	{
		this.download.addActionListener(al);
	}

	public void setShowPropertiesListener(ActionListener al)
	{
		this.showProperties.addActionListener(al);
	}

	public void setRenameListener(ActionListener al)
	{
		this.rename.addActionListener(al);
	}

	public void setCutListener(ActionListener al)
	{
		this.cut.addActionListener(al);
	}

	public void setCopyListener(ActionListener al)
	{
		this.copy.addActionListener(al);
	}

	public void setPasteListener(ActionListener al)
	{
		this.paste.addActionListener(al);
	}

	public void setDeleteListener(ActionListener al)
	{
		this.delete.addActionListener(al);
	}

	public void setLogAnsehenListener(ActionListener al)
	{
		this.showLog.addActionListener(al);
	}

	private void InitForm()
	{
		setTitle("ShareBox");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc;
		setLayout(gbl);

		title = new JLabel("Willkommen in ShareBox!");
		gbc = makegbc(0, 0, 0, 1);
		gbc.fill = GridBagConstraints.NONE;
		gbl.setConstraints(title, gbc);
		add(title);

		explorer = new JList(dlm);
		gbc = makegbc(0, 1, 2, 12);
		gbc.fill = GridBagConstraints.BOTH;
		gbl.setConstraints(explorer, gbc);
		add(explorer);

		createFolder = new JButton("Folder anlegen");
		gbc = makegbc(2, 1, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(createFolder, gbc);
		add(createFolder);
		createFolder.setEnabled(true);

		uploadFile = new JButton("File hochladen");
		gbc = makegbc(2, 2, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(uploadFile, gbc);
		add(uploadFile);
		uploadFile.setEnabled(true);

		download = new JButton("download");
		gbc = makegbc(2, 3, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(download, gbc);
		add(download);
		download.setEnabled(false);

		showProperties = new JButton("Eigenschaften anzeigen");
		gbc = makegbc(2, 4, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(showProperties, gbc);
		add(showProperties);
		showProperties.setEnabled(false);

		rename = new JButton("rename");
		gbc = makegbc(2, 5, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(rename, gbc);
		add(rename);
		rename.setEnabled(false);

		cut = new JButton("cut");
		gbc = makegbc(2, 6, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(cut, gbc);
		add(cut);
		cut.setEnabled(false);

		copy = new JButton("copy");
		gbc = makegbc(2, 7, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(copy, gbc);
		add(copy);
		copy.setEnabled(false);

		paste = new JButton("paste");
		gbc = makegbc(2, 8, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(paste, gbc);
		add(paste);
		paste.setEnabled(false);

		delete = new JButton("delete");
		gbc = makegbc(2, 9, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(delete, gbc);
		add(delete);
		delete.setEnabled(false);

		sendInvitation = new JButton("Einladung verschicken");
		gbc = makegbc(2, 10, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(sendInvitation, gbc);
		add(sendInvitation);
		sendInvitation.setEnabled(false);

		showLog = new JButton("Log ansehen");
		gbc = makegbc(2, 11, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(showLog, gbc);
		add(showLog);
		showLog.setEnabled(true);

		logout = new JButton("logout");
		gbc = makegbc(2, 12, 1, 1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(logout, gbc);
		add(logout);
		logout.setEnabled(true);

		pack();
	}

	/**
	 * Hilfsmethode fuer GridBagConstraints. Es kann die Position der Elemente
	 * festgelegt werden
	 *
	 * @param x -
	 *          Spalte
	 * @param y -
	 *          Zeile
	 * @param width -
	 *          Breite des Elements
	 * @param height -
	 *          Hoehe des Elements
	 * @return gbc - Rueckgabe des gbc (GridBagConstraints) objekts
	 */
	public GridBagConstraints makegbc(int x, int y, int width, int height)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.insets = new Insets(2, 8, 2, 8); // Rand festlegen = Mindestabstand zu
		// anderen Elementen
		return gbc;
	}

}
